package com.copa.l1233;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author copa
 * @createDate 2023-02-08 09:12
 * @function 排序法
 */
public class Test1 {

    public List<String> removeSubfolders(String[] folder) {
        Arrays.sort(folder);
        List<String> result = new ArrayList<>();
        result.add(folder[0]);
        for (int i = 1; i < folder.length; i++) {
            int frontSize = result.get(result.size() - 1).length();
            if (frontSize < folder[i].length()
                    && result.get(result.size() - 1).equals(folder[i].substring(0, frontSize))
                    && folder[i].charAt(frontSize) == '/') {
                // 如果当前字符串长度小于结果集最后一位字符串的长度 且 结果集最后一位的字符串等于当前字符串的前缀 且 在当前长度字符串中的指定的结果集最后一位字符串的长度索引等于“/”
                continue;
            }
            result.add(folder[i]);
        }
        return result;
    }

    public static void main(String[] args) {
        String[] folder = {"/a","/a/b","/c/d","/c/d/e","/c/f"};
        List<String> result = new Test1().removeSubfolders(folder);
        for (String s : result) {
            System.out.println(s);
        }
    }
}
